package dao;

import domain.VirtualFile;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface FileDao {

    @Insert("insert into file values (null,#{fname},#{dept},#{speciality},#{description},(select uid from user where uname=#{uploader}),#{uploadDateTime},#{MD5})")
    public void save(VirtualFile virtualFile);

    @Select("select fid,fname,dept,speciality,description,user.uname uploader,uploadDateTime,Md5 from file,user where (fname like concat('%',#{fname},'%') or description like concat('%',#{fname},'%')) and file.uploader = user.uid" )
    public List<VirtualFile> getByName_desc(String fname);

    @Select("select * from file where MD5 = #{MD5}")
    public VirtualFile getByMD5(String MD5);

    @Select("select fid,fname,dept,speciality,description,user.uname uploader,uploadDateTime,Md5 from file,user where file.uploader = user.uid")
    public List<VirtualFile> getAll();

    @Select("select fid,fname,dept,speciality,description,user.uname uploader,uploadDateTime,Md5 from file,user where speciality = #{speciality} and file.uploader = user.uid")
    List<VirtualFile> getBySpeciality(String speciality);

    @Select("select * from file where fid = #{fid}")
    public  VirtualFile getById(Integer id);

    @Update("update file set uploader = #{uid2} where uploader = #{uid}")
    void setNewOwner(@Param("uid") int uid, @Param("uid2") int i);
}
